<template>
  <div class="upload-file">
    <el-upload
      class="upload"
      :action="uploadFileUrl"
      :before-upload="handleBeforeUpload"
      :limit="1"
      :on-exceed="handleExceed"
      :on-error="handleUploadError"
      :on-success="handleUploadSuccess"
      :show-file-list="false"
      :file-list="fileList">
      <el-button size="small" type="primary">上传</el-button>
      <el-button size="small" type="info" @click="handleView" v-show="fileId!=='' && type === 'application/pdf'">预览</el-button>
      <el-button size="small" type="success" @click="handleDownload" v-show="fileId!==''">下载</el-button>
      <div slot="tip" class="el-upload__tip">
        只能上传
        <template v-if="fileSize"> 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b> </template>
        <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
        的文件
      </div>
    </el-upload>
  </div>
</template>

<script>
  export default {
    props: {
      // 大小限制(MB)
      fileSize: {
        type: Number,
        default: 10,
      },
      // 文件类型, 例如['png', 'jpg', 'jpeg']
      fileType: {
        type: Array,
        default: () => ["doc", "xls", "ppt", "txt", "pdf"],
      },
      fileId: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        uploadFileUrl: this.$http.adornUrl("/api-file/file/upload"), // 上传的服务器地址
        fileList: [],
      };
    },
    methods: {
      // 上传前校检格式和大小
      handleBeforeUpload(file) {
        // 校检文件类型
        if (this.fileType) {
          let fileExtension = "";
          if (file.name.lastIndexOf(".") > -1) {
            fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
          }
          const isTypeOk = this.fileType.some((type) => {
            if (file.type.indexOf(type) > -1) return true;
            if (fileExtension && fileExtension.indexOf(type) > -1) return true;
            return false;
          });
          if (!isTypeOk) {
            this.$message.error(`文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`);
            return false;
          }
        }
        // 校检文件大小
        if (this.fileSize) {
          const isLt = file.size / 1024 / 1024 < this.fileSize;
          if (!isLt) {
            this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
            return false;
          }
        }
        return true;
      },
      // 文件个数超出
      handleExceed() {
        this.$message.error(`只允许上传单个文件`);
      },
      // 上传失败
      handleUploadError(err) {
        this.$message.error("上传失败, 请重试");
      },
      // 上传成功回调
      handleUploadSuccess(res, file) {
        this.$message.success("上传成功");
        this.$emit("input", res.data);
        this.fileList = [];
      },
      handleDownload() {
        window.open(this.$http.adornUrl('/api-file/file/download/file?fileId=' + this.fileId))
      },
      handleView() {
        window.open(this.$http.adornUrl('/api-file/file/view/file?fileId='+ this.fileId))
      }
    },

  };
</script>

<style scoped lang="scss">
  .upload-file-uploader {
    margin-bottom: 5px;
  }
  .upload-file-list .el-upload-list__item {
    border: 1px solid #e4e7ed;
    line-height: 2;
    margin-bottom: 10px;
    position: relative;
  }
  .upload-file-list .ele-upload-list__item-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: inherit;
  }
  .ele-upload-list__item-content-action .el-link {
    margin-right: 10px;
  }
</style>
